package com.himiotech.core.project.web.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.constant.Constants;
import com.himiotech.core.framework.helper.constant.UserConstants;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.utils.ExcelUtil;
import com.himiotech.core.framework.helper.utils.ShiroUtils;
import com.himiotech.core.project.model.entity.Config;
import com.himiotech.core.project.model.entity.user.User;
import com.himiotech.core.project.service.IConfigService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Controller
@RequestMapping("/system/config")
public class ConfigController extends HimioBaseController {

    @Autowired
    private IConfigService configService;

    @RequiresPermissions("system:config:view")
    @GetMapping
    public String config() {
        return "system/config";
    }

    /**
     * 查询参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @PostMapping("/list")
    @ResponseBody
    public HimioResult list(Page<Config> page, Config config) {
        handleOrderBy(request, page);
        User sysUser = getSysUser();
        if (sysUser == null || !sysUser.isAdmin()) {
            config.setConfigType(Constants.SYSTEM_NO_DEFAULT);
        }
        page = configService.selectConfigListPage(page, config);
        return writeJsonLayuiTable(page);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    @ResponseBody
    public HimioResult export(Config config) {
        Page<Config> configPage = new Page<>(1, 65536);
        configPage = configService.selectConfigListPage(configPage, config);
        ExcelUtil<Config> util = new ExcelUtil<>(Config.class);
        return util.exportExcel(configPage.getRecords(), "参数数据");
    }


    /**
     * 新增保存参数配置
     */
    @RequiresPermissions("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public HimioResult addSave(@Validated Config config) {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return writeJsonErr("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return writeJson(configService.insertConfig(config));
    }


    /**
     * 修改保存参数配置
     */
    @RequiresPermissions("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public HimioResult editSave(@Validated Config config) {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return writeJsonErr("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return writeJson(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public HimioResult remove(String ids) {
        configService.deleteConfigByIds(ids);
        return writeJsonOk();
    }

    /**
     * 清空缓存
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @GetMapping("/refreshCache")
    @ResponseBody
    public HimioResult refreshCache() {
        configService.resetConfigCache();
        return writeJsonOk();
    }

    /**
     * 校验参数键名
     */
    @PostMapping("/checkConfigKeyUnique")
    @ResponseBody
    public String checkConfigKeyUnique(Config config) {
        return configService.checkConfigKeyUnique(config);
    }
}
